﻿using System;
using System.Collections;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Xml.Linq;
using System.Collections.Generic;
using System.Text;
using System.Web.SessionState;

namespace Jy123.Web.Module.Venues
{

    public class VenuesRaceProps : IHttpHandler, IReadOnlySessionState
    {
        private Jy123.Model.Venues_Main venues;
        private Jy123.Model.Club_Main club;
        private Jy123.Model.Public_Level level;
        private Jy123.Model.User_Main user;
        public void ProcessRequest(HttpContext context)
        {
            string strUserIdFromCookie = Common.Cookie.getCookie("UserID");
            if (string.IsNullOrEmpty(strUserIdFromCookie))
            {
                context.Response.Redirect("/");
            }
            string key = "VenuesId";
            string venuesIdString = context.Request.QueryString[key];
            int venuesId = int.Parse(venuesIdString);

            Jy123.BLL.Venues_Main BLLVenues = new Jy123.BLL.Venues_Main();
            Jy123.BLL.Club_Main BLLClub = new Jy123.BLL.Club_Main();
            Jy123.BLL.Public_Level BLLLevel = new Jy123.BLL.Public_Level();
            Jy123.BLL.User_Main BLLUser = new Jy123.BLL.User_Main();

            venues = BLLVenues.GetModel(venuesId);
            user = BLLUser.GetModel(int.Parse(strUserIdFromCookie));
            level = BLLLevel.GetModel(venues.FLevel.Value);
            string mark = context.Request.QueryString["CMark"];
            if (string.IsNullOrEmpty(mark))
            {
                mark = context.Request["CMark"];
            }
            if (!string.IsNullOrEmpty(mark))
            {
                string strPropsId = context.Request.QueryString["PropsId"];
                if (!string.IsNullOrEmpty(strPropsId))
                {
                    int pId = int.Parse(strPropsId);
                    Jy123.BLL.Public_Game BLLGame = new Jy123.BLL.Public_Game();
                    string strGameWhere = string.Format("FVenues = {0} AND IsEnd = 0", venues.ID.ToString());
                    string[] strClubId = venues.RaceClub.Split(',');
                    if (strClubId.Length > int.Parse(mark))
                    {
                        int cId = int.Parse(strClubId[int.Parse(mark)]);
                        Model.Club_Main club = BLLClub.GetModel(cId);
                        List<Jy123.Model.Public_Game> listGame = BLLGame.GetModelList(strGameWhere);
                        if (listGame.Count > 0)
                        {
                            BLL.Props.Props BLLProps = new Jy123.BLL.Props.Props();
                            BLLProps.UsePropsToClub(user, club, listGame[0], pId);
                        }
                    }

                }
                string strResult = GetPropsString(context);
                context.Response.Write(strResult);
            }

        }

        private string GetPropsString(HttpContext context)
        {
            string propsString = context.Cache["venuesracepropsdiv"].ToString();
            BLL.Public_Props BLLProps = new Jy123.BLL.Public_Props();
            BLL.Props.Props BLLRecord = new Jy123.BLL.Props.Props();
            List<Model.Public_Props> listProps = BLLProps.GetModelList("FPType = 6");
            Dictionary<int, Model.Public_Props> dicProps = new Dictionary<int, Jy123.Model.Public_Props>();
            foreach (Model.Public_Props props in listProps)
            {
                dicProps.Add(props.ID, props);
            }
            List<Model.Club_PropsRecord> listRecords = BLLRecord.GetPropsRecord(user.ID, 0, "6");
            StringBuilder sbString = new StringBuilder();
            string[] strClubId = venues.RaceClub.Split(',');
            string cMark = context.Request.QueryString["CMark"];
            if (string.IsNullOrEmpty(cMark))
            {
                cMark = context.Request["CMark"];
            }
            foreach (Model.Club_PropsRecord record in listRecords)
            {
                string strLi = context.Cache["venuesracepropsli"].ToString();
                Model.Public_Props cProps = dicProps[record.FProps.Value];
                strLi = strLi.Replace("$HiddenId$", "hidden" + cProps.ID);
                strLi = strLi.Replace("$Value$", cProps.ID.ToString());
                strLi = strLi.Replace("$ImageUrl$", cProps.Model);
                strLi = strLi.Replace("$ImageId$", "image" + cProps.ID.ToString());
                strLi = strLi.Replace("$Alt$", cProps.PName + " 数量：" + record.Quantity.ToString());
                StringBuilder sbClick = new StringBuilder();
                if (venues.VStatus == Convert.ToInt32(Jy123.BLL.Public.EnumConfig.VenuesStatus.Race))
                {
                    //正在比赛
                    string lrInfo = "fillhtml('raceInfoDiv0','/Module/Venues/VenuesRaceInfo.ashx?VenuesId=" + venues.ID.ToString() + "&Mark=0&PropsId=" + record.FProps.Value.ToString() + "'+'&'+ Math.random()),";
                    string rrInfo = "fillhtml('raceInfoDiv1','/Module/Venues/VenuesRaceInfo.ashx?VenuesId=" + venues.ID.ToString() + "&Mark=1&PropsId=" + record.FProps.Value.ToString() + "'+'&'+ Math.random()),";
                    string lpInfo = "fillhtml('propsInfoDiv0','/Module/Venues/VenuesRaceProps.ashx?VenuesId=" + venues.ID.ToString() + "&Mark=0&CMark=0&PropsId=" + record.FProps.Value.ToString() + "'+'&'+ Math.random()),";
                    string rpInfo = "fillhtml('propsInfoDiv1','/Module/Venues/VenuesRaceProps.ashx?VenuesId=" + venues.ID.ToString() + "&Mark=1&CMark=1&PropsId=" + record.FProps.Value.ToString() + "'+'&'+ Math.random()),";
                    string lpInfoNoUse = "fillhtml('propsInfoDiv0','/Module/Venues/VenuesRaceProps.ashx?VenuesId=" + venues.ID.ToString() + "&CMark=0'+'&'+ Math.random()),";
                    string rpInfoNoUse = "fillhtml('propsInfoDiv1','/Module/Venues/VenuesRaceProps.ashx?VenuesId=" + venues.ID.ToString() + "&CMark=1'+'&'+ Math.random()),";
                    string lcInfo = "fillhtml('clubInfoDiv0','/Module/Venues/VenuesClubInfo.ashx?VenuesId=" + venues.ID.ToString() + "&Mark=0&random='+ Math.random()),";
                    string rcInfo = "fillhtml('clubInfoDiv1','/Module/Venues/VenuesClubInfo.ashx?VenuesId=" + venues.ID.ToString() + "&Mark=1&random='+ Math.random())";
                    sbClick.Append("$(function(){");
                    if (cMark.Equals("0"))
                    {
                        sbClick.Append(lpInfo);
                        sbClick.Append(rpInfoNoUse);
                    }
                    else
                    {
                        sbClick.Append(rpInfo);
                        sbClick.Append(lpInfoNoUse);
                    }
                    sbClick.Append(lrInfo);
                    sbClick.Append(rrInfo);
                    sbClick.Append(lcInfo);
                    sbClick.Append(rcInfo);
                    sbClick.Append("})");
                }
                else
                {
                    string strClick = "javascript:$.moonbox.warning('比赛尚未开始或者已经结束');return false;";
                    sbClick.Append(strClick);
                }

                strLi = strLi.Replace("$Click$", sbClick.ToString());
                string strHelp = "加油";
                int value = 0;
                if (cProps.Liveness.HasValue)
                {
                    value = value + cProps.Liveness.Value;
                }
                if (cProps.Moral.HasValue)
                {
                    value = value + cProps.Moral.Value;
                }
                if (cProps.PhysicalFitness.HasValue)
                {
                    value = value + cProps.PhysicalFitness.Value;
                }
                if (cProps.Skill.HasValue)
                {
                    value = value + cProps.Skill.Value;
                }
                if (value < 0)
                {
                    strHelp = "捣乱";
                }
                strLi = strLi.Replace("$Action$", strHelp);
                sbString.Append(strLi);
            }
            propsString = propsString.Replace("$Li$", sbString.ToString());
            return propsString;
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}
